﻿package plugin.MeiRiLeiChong.game
{
    import cmd.*;
    import com.core.*;
    import com.sh.game.display.*;
    import flash.events.*;
    import flash.external.*;
    import flash.geom.*;
    import manager.*;
    import morn.core.components.*;
    import morn.core.handlers.*;
    import plugin.MeiRiLeiChong.control.*;
    import uiresource.*;
    import view.mornExtends.*;
    import view.uilt.*;

    public class MeiRiLeiChongUICtrl extends MeiRiLeiChongUI
    {
        private var _close:Function;
        private var LiBaoArr:Array;
        private var SelectLiBao:int = 0;
        private var myYuanbao:int = 0;
        private var curDang:int = 1;
        private var mPowerNumber:NumberComponent;
        private var cfgDayPay:Array;
        private var cfgItems:Object;
        private var cfgBox:Object;
        private var mc:XMLMovieClip;

        public function MeiRiLeiChongUICtrl(param1:Function)
        {
            this.cfgDayPay = ConfigDictionary.data.daypayreward;
            this.cfgItems = ConfigDictionary.data.item;
            this.cfgBox = ConfigDictionary.data.box;
            this._close = param1;
            return;
        }// end function

        override protected function initializeView() : void
        {
            super.initializeView();
            var _loc_1:* = Control.create(MeiRiLeiChongControl, this);
            ModelProxy.addObserver("mrlc", _loc_1);
            XMLMcManager.instance.complatefuncs["mrlc"] = this.mcCompletefunc;
            this.mPowerNumber = new NumberComponent();
            this.mPowerNumber.number = 0;
            this.mPowerNumber.y = 13;
            panel.addChild(this.mPowerNumber);
            descLabel.text = "每日0点刷新，1RMB = 100元宝";
            moneyLabel.visible = false;
            this.initEvent();
            return;
        }// end function

        private function mcCompletefunc() : void
        {
            this.mc = XMLMcManager.instance.getPluginMc("mrlc", "mrlc");
            if (this.mc)
            {
                this.mc.x = 408;
                this.mc.y = 355;
                this.mc.interval = 1000 / 8;
                this.mc.play();
                this.mc.mouseEnabled = false;
                this.addChild(this.mc);
                if (lingquBtn.visible == false)
                {
                    this.mc.visible = true;
                }
                else
                {
                    this.mc.visible = false;
                }
            }
            return;
        }// end function

        public function openRefresh() : void
        {
            this.mcCompletefunc();
            this.showReward();
            return;
        }// end function

        private function initEvent() : void
        {
            lingquBtn.addEventListener(MouseEvent.CLICK, this.lingquHandler);
            chongqianBtn.addEventListener(MouseEvent.CLICK, this.chongqianHandler);
            closeBtn.addEventListener(MouseEvent.CLICK, this.closeHandler);
            return;
        }// end function

        public function showReward() : void
        {
            var _loc_4:Boolean = false;
            var _loc_5:String = null;
            var _loc_6:int = 0;
            var _loc_1:* = UserData.getOnlineData();
            this.myYuanbao = _loc_1.daypay;
            var _loc_2:* = _loc_1.daypayreward;
            phLabel.text = "已充值:" + this.myYuanbao.toString() + "元宝";
            var _loc_3:int = 0;
            while (_loc_3 < 5)
            {
                
                _loc_4 = false;
                for (_loc_5 in _loc_2)
                {
                    
                    if (--int(_loc_5) == _loc_3)
                    {
                        _loc_4 = true;
                        break;
                    }
                }
                if (!_loc_4)
                {
                    this.showDangItems((_loc_3 + 1));
                    return;
                }
                _loc_3++;
            }
            if (--int(_loc_5) == 4)
            {
                this.close();
                ModelProxy.change("mainui", "removeBtn", "mrlc");
            }
            this.showDangItems(5);
            return;
        }// end function

        private function showDangItems(param1:int) : void
        {
            var _loc_3:Object = null;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_9:int = 0;
            this.curDang = param1;
            this.LiBaoArr = new Array();
            while (this.LiBaoArr.length > 0)
            {
                
                this.LiBaoArr.shift();
            }
            var _loc_2:* = ModelProxy.getValue("data", "openday");
            if (_loc_2 > 4)
            {
                _loc_2 = 4;
            }
            for each (_loc_3 in this.cfgDayPay)
            {
                
                if (int(_loc_3.type) == this.curDang && int(_loc_3.openday) == (_loc_2 + 1))
                {
                    this.LiBaoArr.push(_loc_3);
                }
            }
            phLabel.text = "礼包总价值：" + this.cfgDayPay[param1 + "_" + (_loc_2 + 1)].value + "元宝";
            moneyLabel.text = "已充值：" + String(this.myYuanbao) + "元宝";
            _loc_4 = int(this.LiBaoArr[0].money);
            _loc_5 = 0;
            if (_loc_4 - this.myYuanbao < 0)
            {
                _loc_5 = 0;
            }
            else
            {
                _loc_5 = _loc_4 - this.myYuanbao;
            }
            this.mPowerNumber.number = _loc_5;
            this.mPowerNumber.x = panel.width - this.mPowerNumber.width >> 1;
            if (this.myYuanbao >= _loc_4)
            {
                lingquBtn.visible = true;
                chongqianBtn.visible = false;
                if (this.mc)
                {
                    this.mc.visible = false;
                }
            }
            else
            {
                lingquBtn.visible = false;
                chongqianBtn.visible = true;
                if (this.mc)
                {
                    this.mc.visible = true;
                }
            }
            this.showItem(0);
            var _loc_6:int = 0;
            var _loc_7:Array = [];
            var _loc_8:* = UserData.getOnlineData();
            if (UserData.getOnlineData().daypayreward)
            {
                for each (_loc_9 in _loc_8.daypayreward)
                {
                    
                    if (_loc_9 == 1)
                    {
                        _loc_7.push(_loc_9);
                    }
                }
                if (_loc_7.length == 5)
                {
                    return;
                }
                _loc_6 = this.cfgDayPay[(_loc_7.length + 1) + "_" + (_loc_2 + 1)].money;
            }
            else
            {
                _loc_6 = this.cfgDayPay["1_" + (_loc_2 + 1)].money;
            }
            if (this.myYuanbao >= _loc_6)
            {
                ModelProxy.change("mainui", "showEffect", {ab:"mrlc", state:true});
            }
            else
            {
                ModelProxy.change("mainui", "showEffect", {ab:"mrlc", state:false});
            }
            return;
        }// end function

        private function showItem(param1:int) : void
        {
            this.SelectLiBao = param1;
            var _loc_2:* = this.LiBaoArr[param1].reward;
            var _loc_3:* = new Array();
            while (_loc_3.length > 0)
            {
                
                _loc_3.shift();
            }
            var _loc_4:* = new Object();
            var _loc_5:* = new Array();
            _loc_3 = ItemUilt.getBoxArr(_loc_2);
            list.array = _loc_3;
            list.renderHandler = new Handler(this.renderHandler);
            return;
        }// end function

        private function renderHandler(param1:Component, param2:int) : void
        {
            if (param2 < list.length)
            {
                (param1 as FlashItem).effshow = true;
                (param1 as FlashItem).data = list.array[param2];
            }
            return;
        }// end function

        private function leftHandler(event:MouseEvent) : void
        {
            var _loc_2:String = this;
            var _loc_3:* = this.SelectLiBao - 1;
            _loc_2.SelectLiBao = _loc_3;
            if (this.SelectLiBao < 0)
            {
                this.SelectLiBao = 2;
            }
            this.showItem(this.SelectLiBao);
            return;
        }// end function

        private function rightHandler(event:MouseEvent) : void
        {
            var _loc_2:String = this;
            var _loc_3:* = this.SelectLiBao + 1;
            _loc_2.SelectLiBao = _loc_3;
            if (this.SelectLiBao > 2)
            {
                this.SelectLiBao = 0;
            }
            this.showItem(this.SelectLiBao);
            return;
        }// end function

        private function chongqianHandler(event:MouseEvent) : void
        {
            if (!ItemUilt.canpay)
            {
                ModelProxy.change("movetips", "error", "功能尚未开放，敬请期待");
                return;
            }
            ExternalInterface.call("topay");
            return;
        }// end function

        private function lingquHandler(event:MouseEvent) : void
        {
            var _loc_2:Array = null;
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_7:Point = null;
            if (_loc_2)
            {
                while (_loc_2.length > 0)
                {
                    
                    _loc_2.pop();
                }
                _loc_2 = null;
            }
            _loc_2 = [];
            var _loc_3:int = 0;
            while (_loc_3 < list.array.length)
            {
                
                _loc_5 = 308 + _loc_3 * (70 + 3);
                _loc_6 = 333;
                _loc_7 = localToGlobal(new Point(_loc_5, _loc_6));
                _loc_2[_loc_3] = {x:_loc_7.x, y:_loc_7.y};
                _loc_3++;
            }
            ModelProxy.change("data", "mrlcItemPoint", _loc_2);
            var _loc_4:* = this.curDang;
            PluginManager.instance.sendToServer("xunbao", CMDXunBao.getDayPayReward, _loc_4);
            return;
        }// end function

        private function closeHandler(event:MouseEvent) : void
        {
            this.close();
            return;
        }// end function

        private function close() : void
        {
            if (this._close != null)
            {
                this._close();
            }
            if (this.mc && this.mc.parent)
            {
                this.mc.parent.removeChild(this.mc);
            }
            return;
        }// end function

    }
}
